﻿using Microsoft.AspNetCore.Components.Web;
using MiniFox.Infrastructure;
using MiniFox.Utility;
using MiniFox.Web;
using Newtonsoft.Json;
using NRedisStack.Graph.DataTypes;
using System.Collections.Generic;
using System.Linq;

namespace MiniFox.Platform.File.Models
{
    /// <summary>
    /// 
    /// </summary>
    [JsonObject]
    public class FileData
    {
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("fileId")]
        public string FileId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("formName")]
        public string FormName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("fileName")]
        public string FileName { get; set; }

        /// <summary>
        /// MIME类型
        /// </summary>
        [JsonProperty("contentType")]
        public string ContentType { get; set; } = WebUtil.MIME_TYPE_JSON; 

        /// <summary>
        /// 文件扩展名
        /// </summary>
        [JsonProperty("extensionName")]
        public string ExtensionName
        {
            get
            {
                return System.IO.Path.GetExtension(this.FileName);
            }
        }

        /// <summary>
        /// 文件显示名，不带扩展名
        /// </summary>
        [JsonProperty("displayName")]
        public string DisplayName
        {
            get
            {
                return System.IO.Path.GetFileNameWithoutExtension(this.FileName);
            }
        }

        /// <summary>
        /// 目录
        /// </summary>
        [JsonProperty("path")]
        public string Path { get; set; } = "/";

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("group")]
        public string Group { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("createTime"), JsonConverter(typeof(DateTimeJsonConverter), GlobalStandardFormat.SIMPLE_DATETIME_FORMAT)]
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("updateTime"), JsonConverter(typeof(DateTimeJsonConverter), GlobalStandardFormat.SIMPLE_DATETIME_FORMAT)]
        public DateTime UpdateTime { get; set; }

        private byte[] _data;
        /// <summary>
        /// 文件数据内容
        /// </summary>
        [JsonIgnore]
        public byte[] Data 
        {
            get{
                return _data;
            }
            set
            {
                _data = value;
                if (_data != null)
                    this.ContentLength = _data.LongLength; 
            }
        }

        /// <summary>
        /// 文件流长度
        /// </summary>
        [JsonProperty("contentLength")]
        public long ContentLength { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [JsonIgnore]
        protected internal bool HasContent
        {
            get
            {
                return this.Data != null && this.Data.LongLength > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [JsonProperty("fullName")]
        public virtual string FullName
        {
            get
            {
                string path = this.Path.TrimEnd('/').Trim();
                string fileName = this.FileName.Trim('/').Trim();
                string fullName = string.IsNullOrWhiteSpace(path) ? $"/{fileName}" : $"{path}/{fileName}";
                return fullName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileData"></param>
        public static implicit operator (string formName, string fileName, string mimeType, byte[] data)(FileData fileData)
        {
            return (fileData.FormName, fileData.FileName, fileData.ContentType, fileData.Data);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file"></param>
        public static implicit operator FileData((string formName, string fileName, string mimeType, byte[] data) file)
        {
            return new FileData
            {
                FormName = file.formName,
                FileName= file.fileName,
                ContentType= file.mimeType,
                Data= file.data
            };
        }
    }
}